package com.edgclub.edg.fragment;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.graphics.drawable.AnimationDrawable;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.edgclub.edg.R;
import com.umeng.analytics.MobclickAgent;


/**
 * fragment基类
 *
 */
public class GzBaseFragment extends Fragment implements DialogInterface.OnCancelListener{

	private ProgressDialog dialog;

	//加载动画
	private LinearLayout ll_load;
	private ImageView imgv_load;
	private TextView tv_load,tv_reload;
	private AnimationDrawable animationDrawable;

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onPageStart(this.getClass().getSimpleName()); // 统计页面
	}
	
	
	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPageEnd(this.getClass().getSimpleName());
	}

	/**
	 * 显示dialog
	 *
	 * @param message
	 */
	public void showDialog(String message, boolean isCanCancel) {

		if (dialog == null) {
			dialog = new ProgressDialog(getActivity());
		}
		try {
			dialog.setMessage(message);
			dialog.setCanceledOnTouchOutside(isCanCancel);
			dialog.setOnCancelListener(this);
			dialog.show();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


	/**
	 * 取消dialog
	 */
	public void dismissDialog() {
		if (dialog != null) {
			dialog.dismiss();
			dialog = null;
		}
	}

	protected void dialogCancel(DialogInterface dialog) {
		if (null != dialog) {
			dialog.dismiss();
		}
	}

	@Override
	public void onCancel(DialogInterface dialog) {
		dialogCancel(dialog);
	}

	/**
	 * 默认加载动画
	 * @param view
     */
	public void initLoadView(View view){
		ll_load = (LinearLayout) view.findViewById(R.id.ll_load);
		ll_load.setBackgroundColor(getResources().getColor(R.color.c9));
		ll_load.setVisibility(View.VISIBLE);
		imgv_load = (ImageView) view.findViewById(R.id.imgv_load);
		tv_load = (TextView) view.findViewById(R.id.tv_load);
		tv_reload = (TextView) view.findViewById(R.id.tv_reload);
		animationDrawable = (AnimationDrawable) imgv_load.getDrawable();
		animationDrawable.start();
	}

	public void setLoadViewGONE(){
		animationDrawable.stop();
		ll_load.setVisibility(View.GONE);
	}

	public void setNoNetWork(final ReLoadClickListener reLoadClickListener){
		imgv_load.setImageResource(R.drawable.no_network);
		tv_reload.setVisibility(View.VISIBLE);
		tv_load.setText("网络你去哪里了呀");
		ll_load.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				if(reLoadClickListener!=null){
					imgv_load.setImageResource(R.drawable.loading_default);
					tv_reload.setVisibility(View.GONE);
//					animationDrawable.start();
					tv_load.setText("正在努力加载中...");
					reLoadClickListener.reload();
				}
			}
		});
	}


	public interface ReLoadClickListener {
		void reload();
	}



}
